You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在Kendo UI Grid行弹窗内添加文件附件?替换文件名显示为“file attachment”

Absolutely! You can definitely add a file attachment field to your Kendo UI Grid's row popup editor, and replace the dynamic filename with the fixed text "file attachment". Here's a step-by-step breakdown to make this work smoothly:

1. Set up the Popup Editor with a File Upload Field

First, define your Grid's popup editor template to include a file input and a fixed display element. We'll use a custom template to control exactly what's shown:

<!-- Popup editor template -->
<script id="row-popup-template" type="text/x-kendo-template">
  <!-- Your existing form fields go here -->
  <div class="k-edit-label">
    <label for="attachment">Attachment</label>
  </div>
  <div class="k-edit-field">
    <input type="file" name="attachment" id="attachment-input" />
    <!-- Fixed text display -->
    <span class="file-label">file attachment</span>
  </div>
</script>

<!-- Grid initialization -->
<script>
$("#your-grid").kendoGrid({
  dataSource: yourDataSource,
  editable: {
    mode: "popup",
    template: kendo.template($("#row-popup-template").html())
  },
  columns: [
    // Your other columns
    { 
      field: "attachment", 
      title: "Attachment", 
      template: "<span>file attachment</span>" 
    }
  ]
});
</script>
2. Handle File Selection Without Changing the Display

We need to capture the selected file for upload, but keep the "file attachment" text visible no matter what file the user picks. Add this event listener:

$("#your-grid").on("change", "#attachment-input", function(e) {
  const selectedFile = e.target.files[0];
  if (selectedFile) {
    // Get the current editable data item to store the file
    const grid = $("#your-grid").data("kendoGrid");
    const dataItem = grid.editable.options.dataItem;
    
    // Store the file object for later upload
    dataItem.selectedAttachment = selectedFile;
    
    // Force the display text to stay fixed
    $(this).siblings(".file-label").text("file attachment");
  }
});
3. Handle File Upload on Save

When the user saves the popup, we'll manually upload the file using FormData (since Kendo Grid's default save doesn't handle file uploads natively):

$("#your-grid").on("save", function(e) {
  const dataItem = e.model;
  
  // Check if a file was selected
  if (dataItem.selectedAttachment) {
    const formData = new FormData();
    formData.append("attachment", dataItem.selectedAttachment);
    // Add other form data you need to submit
    formData.append("recordId", dataItem.id);
    
    // Send the file to your server endpoint
    $.ajax({
      url: "/api/your-upload-endpoint",
      type: "POST",
      data: formData,
      processData: false,
      contentType: false,
      success: function(response) {
        // Refresh the Grid after successful upload
        e.sender.dataSource.sync();
        alert("Attachment uploaded successfully!");
      },
      error: function(xhr) {
        console.error("Upload failed:", xhr);
        alert("There was an error uploading your attachment.");
      }
    });
    
    // Prevent the default Grid save behavior since we're handling upload manually
    e.preventDefault();
  }
});
Bonus: Reset the Field When Opening the Popup

Make sure the file input and display are reset every time the popup opens, even if a file was selected before:

$("#your-grid").on("edit", function(e) {
  $("#attachment-input").val("");
  $(".file-label").text("file attachment");
  // Clear the stored file from the data item
  e.model.selectedAttachment = null;
});

If you're using Kendo's built-in kendoUpload component instead of a native file input, you can customize its template to show the fixed text instead of filenames—just replace the file input with the Upload component and set its template to <span>file attachment</span>.

Let me know if you hit any roadblocks with this setup—I’m happy to help adjust it to fit your specific use case!

内容的提问来源于stack exchange,提问作者Sruj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:18:38